#!/usr/bin/env python
#
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""The models that are used for the WiFi Testing Dashboard."""
__author__ = "Joey Freeland (joeyfreeland@gmail.com)"

import pickle

from google.appengine.ext import db


class AuthorizedUser(db.Model):
  """This represents an authorized user."""
  email = db.EmailProperty()
  provider = db.StringProperty()


class PickleProperty(db.Property):
  def get_value_for_datastore(self, model_instance):
    value = getattr(model_instance, self.name, None)
    return pickle.dumps(value)

  def make_value_from_datastore(self, value):
    return pickle.loads(str(value))


class Provider(db.Model):
  """This is a simple list of all networks that we track data for."""
  name = db.StringProperty()


class TestResult(db.Expando):
  """This represents a test result from a WiFi Testing Device."""

  def to_dict(self):
    """Return a dictionary representing the model."""
    return dict([(p, unicode(getattr(self, p))) for p in self.properties()])

  aps_per_channel = PickleProperty()
  average_signal = db.StringProperty()
  best_signal = db.StringProperty()
  bing_latency = db.StringProperty()
  biz_down = db.StringProperty()
  biz_latency = db.StringProperty()
  biz_up = db.StringProperty()
  date = db.DateTimeProperty()
  duration = db.StringProperty()
  edns_latency = db.StringProperty()
  external_ip_address = db.StringProperty()
  google_latency = db.StringProperty()
  idns_latency = db.StringProperty()
  internal_ip_address = db.StringProperty()
  local_speedtest_down = db.StringProperty()
  local_speedtest_latency = db.StringProperty()
  local_speedtest_up = db.StringProperty()
  location = db.StringProperty()
  mac_address = db.StringProperty()
  my_channel = db.StringProperty()
  my_signal = db.StringProperty()
  my_speed = db.StringProperty()
  ndt_down = db.StringProperty()
  ndt_up = db.StringProperty()
  neighbors = db.StringProperty()
  provider = db.StringProperty()
  redirect = db.StringProperty()
  remote_speedtest_down = db.StringProperty()
  remote_speedtest_latency = db.StringProperty()
  remote_speedtest_up = db.StringProperty()
  rtsp_time = db.StringProperty()
  ssid = db.StringProperty()
  ssid_count = db.StringProperty()
  temperature = db.StringProperty()
  time_since_last_result = db.StringProperty()
  yahoo_latency = db.StringProperty()


class WiFiTestingDevice(db.Model):
  """This represents the configuration for a WiFi Testing Device."""
  alertable = db.BooleanProperty(default=False)
  config_version = db.StringProperty()
  date_added = db.DateTimeProperty()
  device_name = db.StringProperty()
  last_update = db.DateTimeProperty()
  local_speedtest_name = db.StringProperty()
  local_speedtest_url = db.StringProperty()
  mac_address = db.StringProperty()
  provider = db.StringProperty()
  remote_speedtest_name = db.StringProperty()
  remote_speedtest_url = db.StringProperty()
  software_version = db.StringProperty()
  ssh_host = db.StringProperty()
  ssh_port = db.StringProperty()
  ssh_user = db.StringProperty()
  st_server = db.StringProperty()
